package com.geovis.emergency.spd.controller.basic;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.basic.service.IBasicPunchRuleService;
import com.geovis.emergency.spd.entity.basic.entity.BasicPunchRule;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicPunchRuleQueryDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicPunchRuleSaveDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicPunchRuleUpdateDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
 * <p>
 * 基础模块--打卡规则表 前端控制器
 * </p>
*
* @author chen
* @since 2022-06-20
*/
@RestController
@RequestMapping("/pc/basicPunchRule")
@RequiredArgsConstructor
@Api(value = "基础模块--打卡规则表接口", tags = "基础模块--打卡规则表接口")
@Slf4j
@Validated
public class BasicPunchRuleController extends BaseController<IBasicPunchRuleService> {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_VALUE)
    @PostMapping({"/save"})
    public Result save(@RequestBody @Validated BasicPunchRuleSaveDTO saveDTO) {
        try {
            SimpleDateFormat ft = new SimpleDateFormat("hh:mm");
            Date signInTime=ft.parse(saveDTO.getSignInTime());
            Date signOutTime=ft.parse(saveDTO.getSignOutTime());
            if(signInTime.getTime()>=signOutTime.getTime())
                return Result.failed("签到时间必须小于签退时间");
            Calendar cal = Calendar.getInstance();

            cal.setTime(signInTime);
            cal.add(Calendar.MINUTE,-30);
            saveDTO.setSignInLimitStart(ft.format(cal.getTime()));

            cal.setTime(signInTime);
            cal.add(Calendar.MINUTE,30);
            saveDTO.setSignInLimitEnd(ft.format(cal.getTime()));

            cal.setTime(signOutTime);
            cal.add(Calendar.MINUTE,-30);
            saveDTO.setSignOutLimitStart(ft.format(cal.getTime()));

            cal.setTime(signOutTime);
            cal.add(Calendar.MINUTE,30);
            saveDTO.setSignOutLimitEnd(ft.format(cal.getTime()));

            this.baseService.saveBasicPunchRule(saveDTO);
            return Result.ok();
        } catch (ParseException e) {
            return Result.failed("时间格式不正确");
        }

    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping({"/update"})
    public Result update(@RequestBody @Validated BasicPunchRuleUpdateDTO updateDTO) {

        try {
            SimpleDateFormat ft = new SimpleDateFormat("hh:mm");
            Date signInTime=ft.parse(updateDTO.getSignInTime());
            Date signOutTime=ft.parse(updateDTO.getSignOutTime());
            if(signInTime.getTime()>=signOutTime.getTime())
                return Result.failed("签到时间必须小于签退时间");
            Calendar cal = Calendar.getInstance();

            cal.setTime(signInTime);
            cal.add(Calendar.MINUTE,-30);
            updateDTO.setSignInLimitStart(ft.format(cal.getTime()));

            cal.setTime(signInTime);
            cal.add(Calendar.MINUTE,30);
            updateDTO.setSignInLimitEnd(ft.format(cal.getTime()));

            cal.setTime(signOutTime);
            cal.add(Calendar.MINUTE,-30);
            updateDTO.setSignOutLimitStart(ft.format(cal.getTime()));

            cal.setTime(signOutTime);
            cal.add(Calendar.MINUTE,30);
            updateDTO.setSignOutLimitEnd(ft.format(cal.getTime()));

            this.baseService.updateByIdBasicPunchRule(updateDTO);
            return Result.ok();
        } catch (ParseException e) {
            return Result.failed("时间格式不正确");
        }
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.GET_BY_ID_VALUE, notes = SwaggerConstants.GET_BY_ID_NOTES)
    @PostMapping("/getById/{id}")
    public Result<BasicPunchRule> getById(@PathVariable("id") String id) {
        BasicPunchRule r=baseService.getById(id);
        return Result.ok(r);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量删除", notes = "批量删除数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    @Transactional
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeBatchByIds(idList);
        return Result.ok();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = SwaggerConstants.LIST_PAGE_VALUE, notes = SwaggerConstants.LIST_PAGE_VALUE)
    @PostMapping("/listPage")
    public Result<PageResult<BasicPunchRule>> listPage(@RequestBody PageParam<BasicPunchRuleQueryDTO> pageParam) {
        BasicPunchRuleQueryDTO queryDTO = pageParam.getQuery();
        IPage<BasicPunchRule> basicPunchRulePage = pageParam.buildPage();
        //分页查询
        baseService.page(basicPunchRulePage,baseService.getWrapper(queryDTO));

        return Result.ok(new PageResult<BasicPunchRule>(basicPunchRulePage));
    }
}
